package com.xayy.scheduled.service.impl;

import cn.kawins.mybatis.base.BaseServiceImpl;
import com.github.pagehelper.PageInfo;
import com.xayy.scheduled.CronTaskRegistrar;
import com.xayy.scheduled.SchedulingRunnable;
import com.xayy.scheduled.convert.AssessTaskInfoConvert;
import com.xayy.scheduled.enums.AssessTaskStatus;
import com.xayy.scheduled.mapper.AssessTaskInfoMapper;
import com.xayy.scheduled.model.AssessTaskInfo;
import com.xayy.scheduled.model.dto.AssessTaskInfoDto;
import com.xayy.scheduled.model.vo.AssessTaskInfoVo;
import com.xayy.scheduled.model.vo.AssessTaskInfoVo2;
import com.xayy.scheduled.service.AssessTaskInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @Description: AssessTaskInfoService实现类
* @author zhoutanxin
* @date 2025-06-24
*/
@Service
@AllArgsConstructor
public class AssessTaskInfoServiceImpl extends BaseServiceImpl<AssessTaskInfo> implements AssessTaskInfoService {
        private final AssessTaskInfoMapper assessTaskInfoMapper;
        private final CronTaskRegistrar cronTaskRegistrar;
        @Override
        public PageInfo<AssessTaskInfoVo> page(AssessTaskInfoDto query) {
                List<AssessTaskInfo> list = this.pageList(getWrapper(query),query.getPageNum(),query.getPageSize());

                List<AssessTaskInfoVo> voList=AssessTaskInfoConvert.INSTANCE.convertToVOList(list);

                PageInfo<AssessTaskInfoVo> pageInfo=new PageInfo<AssessTaskInfoVo>(voList);
                pageInfo.setPageNum(query.getPageNum());
                pageInfo.setPageSize(query.getPageSize());
                return pageInfo;
        }

        private Example getWrapper(AssessTaskInfoDto query){
                Example example = new Example(AssessTaskInfo.class);
                if (query.getTaskName()!=null) {
                   example.createCriteria().andEqualTo("taskName", query.getTaskName());
                }
                if (query.getStartTime()!=null) {
                   example.createCriteria().andEqualTo("startTime", query.getStartTime());
                }
                if (query.getEndTime()!=null) {
                   example.createCriteria().andEqualTo("endTime", query.getEndTime());
                }
                if (query.getIndexSystemId()!=null) {
                   example.createCriteria().andEqualTo("indexSystemId", query.getIndexSystemId());
                }
                if (query.getHealthScore()!=null) {
                   example.createCriteria().andEqualTo("healthScore", query.getHealthScore());
                }
                if (query.getHealthAnalysis()!=null) {
                   example.createCriteria().andEqualTo("healthAnalysis", query.getHealthAnalysis());
                }
                if (query.getBeanName()!=null) {
                   example.createCriteria().andEqualTo("beanName", query.getBeanName());
                }
                if (query.getMethodName()!=null) {
                   example.createCriteria().andEqualTo("methodName", query.getMethodName());
                }
                if (query.getMethodParams()!=null) {
                   example.createCriteria().andEqualTo("methodParams", query.getMethodParams());
                }
                if (query.getCronExpression()!=null) {
                   example.createCriteria().andEqualTo("cronExpression", query.getCronExpression());
                }
                if (query.getTaskStatus()!=null) {
                   example.createCriteria().andEqualTo("taskStatus", query.getTaskStatus());
                }
                if (query.getTaskType()!=null) {
                   example.createCriteria().andEqualTo("taskType", query.getTaskType());
                }
                if (query.getRemark()!=null) {
                   example.createCriteria().andEqualTo("remark", query.getRemark());
                }
                if (query.getUpdateTime()!=null) {
                   example.createCriteria().andEqualTo("updateTime", query.getUpdateTime());
                }
                if (query.getCreateTime()!=null) {
                   example.createCriteria().andEqualTo("createTime", query.getCreateTime());
                }

                return example;
        }

        @Override
        public List<AssessTaskInfoVo> listByDTO(AssessTaskInfoDto query) {
                Example queryWrapper=getWrapper(query);
                List<AssessTaskInfo> entityList=this.selectByExample(queryWrapper);;
                return AssessTaskInfoConvert.INSTANCE.convertToVOList(entityList);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public AssessTaskInfo save(AssessTaskInfoVo2 vo2) {
                AssessTaskInfoVo vo = AssessTaskInfoVo.builder()
                        .taskName(vo2.getTaskName())
                        .indexSystemId(Long.valueOf(vo2.getIndexSystemId()))
                        .methodParams(vo2.getIndexSystemId())
                        .cronExpression(vo2.getCronExpression())
                        .remark(vo2.getRemark())
                        .startTime(vo2.getStartTime())
                        .endTime(vo2.getEndTime())
                        .build();
                AssessTaskInfo entity = AssessTaskInfoConvert.INSTANCE.convert(vo);
                entity.setBeanName("assessJob");
                entity.setMethodName("execute");
                entity.setTaskStatus(AssessTaskStatus.PAUSE.value);
                this.insert(entity);
                return entity;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public AssessTaskInfo update(AssessTaskInfoVo vo) {
                AssessTaskInfo entity = AssessTaskInfoConvert.INSTANCE.convert(vo);
                this.updateById(entity);
                return entity;
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public boolean saveBatch(Collection<AssessTaskInfo> entityList) {
                 return this.insertList(new ArrayList<>(entityList))>0;
        }

        @Override
        public List<AssessTaskInfo> getListByStatus(AssessTaskStatus taskStatus) {
                AssessTaskInfoDto sysJobDTO=new AssessTaskInfoDto();
                sysJobDTO.setTaskStatus(taskStatus.getValue());
                Example  queryWrapper=getWrapper(sysJobDTO);
            return this.selectByExample(queryWrapper);
        }

        @Override
        @Transactional(rollbackFor = Exception.class)
        public void delete(List<Long> idList) {
                deleteByIds(idList);
        }

        @Override
        public void toggleStartPause(Long jobId) {
                AssessTaskInfo existedSysJob=this.selectById(jobId);
                if (existedSysJob.getTaskStatus().equals(AssessTaskStatus.PAUSE.getValue())) {
                        SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getId(),existedSysJob.getBeanName(), existedSysJob.getMethodName(), String.valueOf(existedSysJob.getIndexSystemId()));
                        cronTaskRegistrar.addCronTask(task, existedSysJob.getCronExpression());
                        existedSysJob.setTaskStatus(AssessTaskStatus.NORMAL.getValue());
                } else if (existedSysJob.getTaskStatus().equals(AssessTaskStatus.NORMAL.getValue())){
                        SchedulingRunnable task = new SchedulingRunnable(existedSysJob.getId(),existedSysJob.getBeanName(), existedSysJob.getMethodName(), String.valueOf(existedSysJob.getIndexSystemId()));
                        cronTaskRegistrar.removeCronTask(task);
                        existedSysJob.setTaskStatus(AssessTaskStatus.PAUSE.getValue());
                }
                this.updateById(existedSysJob);
        }

}